package org.itboys.core.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.core.entity.mongo.SundayUser;
import org.itboys.core.service.mongo.web.SundayUserService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/8/3
 * @Description：人员管理接口
 */
@Controller
@RequestMapping("/sunday/web/user")
public class SundayUserController extends BaseController{
    @Autowired
    private SundayUserService userService;
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request){
        return "/sunday/user/index";
    }

    @RequestMapping("/input" )
    public  String input(HttpServletRequest request
            ,@RequestParam("id") Long id
            ,Model model){
        SundayUser user = userService.getById(id);
        model.addAttribute("user",user);
        return "/sunday/user/input";
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request
            , HttpServletResponse response){
        try {
            PageResult<SundayUser> result = userService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    @RequestMapping(value = "/save")
    public void save(HttpServletRequest request, HttpServletResponse response
                     ,@RequestParam("id")Long id
                     ,@RequestParam("userName")String userName, @RequestParam("password")String password
                     ,@RequestParam("realName")String realName, @RequestParam("nickName")String nickName
                     ,@RequestParam("mobile")String mobile, @RequestParam("type")Integer type
                     ,@RequestParam("sex")Integer sex, @RequestParam("desc")String desc
                     ,@RequestParam("orgId")Long orgId, @RequestParam("orgName")String orgName
                     ,@RequestParam("status")Integer status,@RequestParam("storeId")Long storeId
                    ,@RequestParam(value = "image",required = false) MultipartFile image){
        try{

            SundayUser user=new SundayUser(userName,password,realName,nickName,mobile,
                                    type,sex, desc,orgId,orgName,status,storeId);
            System.out.println(user.toString());
             userService.save(id, user, image);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/user/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }
    @RequestMapping("/delete")
    public void delete(@RequestParam("id") Long id){
        try{
            userService.delete(id);
            ResponseMessage.success();
            } catch (Exception e) {
                e.printStackTrace();
                String msg = "";
                if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                    msg = e.getMessage();
                } else {
                    msg = CommonConstants.ERROMSG;
                    logger.error("/sunday/web/user/delete------" + e.getMessage());
                }
                e.printStackTrace();
                ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
            }
        }

    /**
     * 仓库选择器，目前业务逻辑只考虑单选
     * @param request
     * @param isSingle
     * @param userType
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="userType",required=false)String userType,//查询参数
                          @RequestParam(value="storeId",required=false)Long storeId,//查询参数
                          Model model){
        model.addAttribute("storeId",storeId);
        model.addAttribute("userType",userType);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/user/choose/choose_erro";
        }
        return "/sunday/user/choose/choose_single";
    }
}
